package com.cbt.cbtBlog.controller;

import com.cbt.cbtBlog.bean.BlogInfo;
import com.cbt.cbtBlog.enums.ResultEnum;
import com.cbt.cbtBlog.service.IBlogInfoService;
import com.cbt.cbtBlog.util.ConstantInfo;
import com.cbt.cbtBlog.vo.ResultVO;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping({"/blog"})
public class BlogInfoController {
   @Autowired
   private IBlogInfoService blogInfoService;

   @RequestMapping({"/findAll"})
   public ResultVO findAll() {
      List<BlogInfo> resultList = this.blogInfoService.findAll();
      return new ResultVO(ResultEnum.SUCCESS, resultList);
   }

   @RequestMapping({"/searchBlog"})
   public ResultVO searchBlog(@RequestParam Map map) {
      List<BlogInfo> resultList = this.blogInfoService.searchContent(map);
      return new ResultVO(ResultEnum.SUCCESS, resultList);
   }

   @RequestMapping({"/getTopBlogs"})
   public ResultVO getTopBlogs() {
      List<BlogInfo> resultList = this.blogInfoService.getTopBlogs();
      return new ResultVO(ResultEnum.SUCCESS, resultList);
   }

   @RequestMapping({"/getBlogInfo"})
   public ResultVO getBlogInfo(BlogInfo blog) {
      BlogInfo blogInfo = this.blogInfoService.getBlogInfo(blog);
      return new ResultVO(ResultEnum.SUCCESS, blogInfo);
   }

   @RequestMapping({"/findByTno"})
   public ResultVO findByTno(@RequestParam Map map) {
      List<BlogInfo> resultList = this.blogInfoService.findByTno(map);
      return new ResultVO(ResultEnum.SUCCESS, resultList);
   }

   @PostMapping({"/upload"})
   public Map<String, Object> upload(MultipartFile upload) {
      String savepath = "blog_files/ckeditor_pics";
      File parent = new File(ConstantInfo.BASEPATH, savepath);
      if (!parent.exists()) {
         parent.mkdirs();
      }

      File dest = null;
      Map<String, Object> map = new HashMap();
      if (upload != null && upload.getSize() > 0L) {
         try {
            savepath = System.currentTimeMillis() + "_" + upload.getOriginalFilename();
            dest = new File(parent, savepath);
            upload.transferTo(dest);
            map.put("fileName", upload.getOriginalFilename());
            map.put("uploaded", 1);
            map.put("url", "../../../file/blog_files/ckeditor_pics/" + savepath);
         } catch (IllegalStateException var7) {
            var7.printStackTrace();
         } catch (IOException var8) {
            var8.printStackTrace();
         }
      }

      return map;
   }

   @PostMapping({"/add"})
   public ResultVO add(BlogInfo pf, MultipartFile[] photos) {
      String savepath = "blog_files/type" + pf.getTno();
      File parent = new File(ConstantInfo.BASEPATH, savepath);
      if (!parent.exists()) {
         parent.mkdirs();
      }

      File dest = null;
      if (photos != null && photos.length > 0 && photos[0].getSize() > 0L) {
         String pics = "";

         try {
            MultipartFile[] var7 = photos;
            int var8 = photos.length;

            for(int var9 = 0; var9 < var8; ++var9) {
               MultipartFile f1 = var7[var9];
               savepath = System.currentTimeMillis() + "_" + f1.getOriginalFilename();
               dest = new File(parent, savepath);
               f1.transferTo(dest);
               if ("".equals(pics)) {
                  pics = "blog_files/type" + pf.getTno() + "/" + savepath;
               } else {
                  pics = pics + ";blog_files/type" + pf.getTno() + "/" + savepath;
               }

               pf.setDefine2(pics);
            }
         } catch (IllegalStateException var11) {
            var11.printStackTrace();
         } catch (IOException var12) {
            var12.printStackTrace();
         }
      }

      int result = this.blogInfoService.add(pf);
      if (result == -1) {
         return new ResultVO(ResultEnum.CHECK_ERROR);
      } else {
         return result > 0 ? new ResultVO(ResultEnum.SUCCESS) : new ResultVO(ResultEnum.ERROR);
      }
   }
}
